package ipcards.rulesets;

import ipcards.CardSet;
import ipcards.Deck;
import ipcards.Card;
import ipcards.Game;
import ipcards.GameBase;
import ipcards.PlayerModel;
import ipcards.Room;
import ipcards.events.PlayerAction;
import ipcards.net.Client;
import ipcards.net.CardsServer;

import java.awt.Point;
import javax.swing.*;

import java.awt.event.*;
import java.util.Vector;





/**
 * Game with no rules. This allows the player(s) to do anything they like within the ipCards engine and not be bound by any rules.
 * This is equivalent to the default state of a session in V0.1.1 and earlier
 * 
 * @author William Jarman
 */
public class FreePlayGame extends GameBase implements Game {
	
	private Room room;
	private FreePlayRuleSet ruleset;
	private CardSet cards;
	private String FPDeckName;

	private ActionListener al;
	public JPanel buttonPanel;
	private JButton shuffleButton;
	private JButton addDeckButton;
	
	public FreePlayGame(PlayerModel pm, Room r, Client source){
		super(pm);
		room = r;
		ruleset = new FreePlayRuleSet(room.controller.getCardServer(), this);
		FPDeckName = ruleset.getDeckName();	
		room.table.addDeck(new Deck(FPDeckName));
			
		cards = room.table.cards;
		if(room.players.size()!=1){
			ruleset.deal(cards);
		}
	}
	
	
	
	public JPanel createButtonPanel(){
		buttonPanel = new JPanel();
		al = new buttonPanelListener();
		shuffleButton = new JButton("shuffle");
			shuffleButton.setMnemonic(KeyEvent.VK_S);
			shuffleButton.addActionListener(al);
		addDeckButton = new JButton("add deck");
			addDeckButton.setMnemonic(KeyEvent.VK_D);
			addDeckButton.addActionListener(al);
		
		buttonPanel.add(shuffleButton);
		buttonPanel.add(addDeckButton);
		return buttonPanel;
	}
	
	public class buttonPanelListener implements ActionListener{
		public void actionPerformed(ActionEvent ae) {
				
			Object source = ae.getSource();
			if(source == shuffleButton){
				shuffle();
			}
			else if(source == addDeckButton){
				addDeck();
			}
		}
	}
	
	public void playerActionPerformed(PlayerAction pa){
		if(room.players.size() != 1){
			//server
		}
		else {
			//player on server
		}
	}
	
	public Room getRoom(){
		return room;
	}
	
	public void shuffle(){
		player.performPlayerAction(PlayerAction.newShuffle(this, cards));
	}
	
	public void addDeck(){
		addDeck(ruleset.getDeckName());
	}
	/*
	JFileChooser loadChooser = new JFileChooser("decks");
	{loadChooser.addChoosableFileFilter(new DeckFileFilter());}
	public void addDeckChoice(){
		int returnVal = loadChooser.showOpenDialog(this);
		if (returnVal == JFileChooser.APPROVE_OPTION) {
			File file = loadChooser.getSelectedFile();
			loadChooser.setCurrentDirectory(file);
			addDeck(file.getName());
		}
	}*/
	
	public void addDeck(String d){
		player.performPlayerAction(new PlayerAction(this, PlayerAction.ADD_DECK, d));				
	}
	
	public boolean legalityCheck(String option){
		return true;
	}
}
